﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZJFramework10.Extension;
using TrainingsApp.Logic;
using TrainingsApp.Model;
using TrainingsApp.WebModel.Route;
namespace TrainingsApp.IPADApp.UserControls.Main.Teacher {
    public partial class UC_Register_Detail : ControlBase {
//        protected override void OnInit(EventArgs e) {
//            TemplateUserInfoManager.Push(new TS_User_Entity {
//                UserName = "Test" + new Random().Next(1000, 9000),
//                UserType = (int)UserType.tea
//,
//            });
//            base.OnInit(e);
//        }
        protected void Page_Load(object sender, EventArgs e) {

            this.dropYear.DataSource = StaticDataSource.ListYear();
            this.dropYear.DataTextField = "Text";
            this.dropYear.DataValueField = "Value";
            this.dropYear.DataBind();

            this.dropMonth.DataSource = StaticDataSource.ListMonth();
            this.dropMonth.DataTextField = "Text";
            this.dropMonth.DataValueField = "Value";
            this.dropMonth.DataBind();

            this.dropDay.DataSource = StaticDataSource.ListDay();
            this.dropDay.DataTextField = "Text";
            this.dropDay.DataValueField = "Value";
            this.dropDay.DataBind();

            this.dropPosition.DataSource = TrainingsApp.Logic.TS_School_Position_Logic.TS_School_Position_ListByPaging(1000, 1).TS_School_Position_List;
            this.dropPosition.DataTextField = "PositionName";
            this.dropPosition.DataValueField = "Idx";
            this.dropPosition.DataBind();

            dropProvince.DataSource = TrainingsApp.Logic.TS_Province_Logic.TS_Province_ListByPaging(1000, 1).TS_Province_List;
            dropProvince.DataTextField = "ProvinceName";
            dropProvince.DataValueField = "Idx";
            dropProvince.DataBind();


            this.dropCourse.DataSource = TrainingsApp.Logic.TS_CourseInfo_Logic.TS_CourseInfo_ListByPaging(100, 1).TS_CourseInfo_List;
            this.dropCourse.DataTextField = "CourseName";
            this.dropCourse.DataValueField = "Idx";
            this.dropCourse.DataBind();



        }


        protected void btnSubmit_Click(object sender, EventArgs args) {

            int classId = ZJFramework10.Utility.FormManager.GetValue<int>("dropClass", 0);
            int schoolId = ZJFramework10.Utility.FormManager.GetValue<int>("dropSchool", 0);

            int courseId = this.dropCourse.SelectedValue.AsInt();
            int jiaocaiId = ZJFramework10.Utility.FormManager.GetValue<int>("dropJiaoCai", 0);
            TS_User_Entity userEntity = null;
            TemplateUserInfoManager.TryGet(out userEntity);


            TS_User_Teacher_Entity teacher = new TS_User_Teacher_Entity();
            teacher.UserName = userEntity.UserName;
            teacher.UserType = (int)userEntity.UserType;
            teacher.SchoolId = schoolId;
            if (this.dropPosition.SelectedIndex > 0)
                teacher.PositionId = this.dropPosition.SelectedValue.AsInt();
            teacher.Password = userEntity.Password;
            teacher.Name = this.txtName.Text;
            teacher.Mobile = userEntity.Mobile;
            teacher.ManagerStatus = 0;
            teacher.Email = userEntity.Email;
            teacher.ClassesId = classId;
            teacher.BirthDay = new DateTime(this.dropYear.SelectedValue.AsInt(), this.dropMonth.SelectedValue.AsInt(), this.dropDay.SelectedValue.AsInt());
            teacher.CourseId = courseId;
            teacher.MaterialsId = jiaocaiId;
            teacher.UserSource = UserSources.Register;
            teacher = TrainingsApp.Logic.TS_User_Teacher_Logic.TS_User_Teacher_Add(new List<TS_User_Teacher_Entity> { 
             teacher
            });

            if (teacher.UserId > 0) {

                var user = Sys_Permission_Logic.GetUserInfo(teacher.UserName);
                TemplateUserInfoManager.Clear();
                Session["CurContext"] = user;
               
                Response.Redirect(this.CurPage.RouteHelper.Route(RouteNames.Success, new { code = SuccessCode.RegisterSuccess }));
            }
        }


    }
}